package com.wjj.ndkdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.wjj.ndkdemo.databinding.ActivityMainBinding;


public class MainActivity extends AppCompatActivity {

    protected String TAG = MainActivity.class.getSimpleName();

    private ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        JniTester tester = new JniTester();


        StringBuffer buffer = new StringBuffer();
        buffer.append(tester.testBaseType(1, 2L, 3.3f, 4.4, true, "这是java的字符串"));
        buffer.append("\n");
        int[] int_arr = new int[]{1, 2, 3, 4};
        String[] str_arr = new String[]{"第一串", "第二串", "第三串"};
        buffer.append(tester.testArray(int_arr, str_arr));
        buffer.append("\n");

        Log.d(TAG, "修改后的数组");
        for (int i = 0; i < int_arr.length; i++) {
            Log.d(TAG, i + ":" + int_arr[i]);
        }
        Log.d(TAG, "修改后的字符串数组");
        for (int i = 0; i < str_arr.length; i++) {
            Log.d(TAG, i + ":" + str_arr[i]);
        }

        TestBean testBean = new TestBean();
        buffer.append(tester.testObjectType(testBean));
        buffer.append("\n");
        Log.d(TAG, "修改类, id: " + testBean.getId());
        Log.d(TAG, "修改类, title: " + testBean.title);
        Log.d(TAG, "修改类, address: " + testBean.address);

        testBean = tester.testNewTestBean();
        if (testBean != null) {
            Log.d(TAG, "jni生成类, id: " + testBean.getId());
            Log.d(TAG, "jni生成类, name: " + testBean.name);
        }

        try {
            tester.testException();
        }catch (Exception e) {
            Log.d(TAG, "捕获到异常"+ e.getLocalizedMessage());
        }

        tester.testThread();

        // Example of a call to a native method
        TextView tv = binding.sampleText;
        tv.setText(buffer.toString());
    }

}